const STORAGE_KEY = "mall";
export default {
  setItem(key,value,module_name) {
      //先拿到user
    if (module_name){
        let val = this.getItem(module_name);
        val[key] = value;
        this.setItem(module_name, val);
      }else{
        let val = this.getStorage();
        val[key] = value;
        window.sessionStorage.setItem(STORAGE_KEY, JSON.stringify(val));
      }
  },
  // 获取某一个模块下面的属性user下面的userName
  getItem(key,module_name){
    if (module_name){
      let val = this.getItem(module_name);//module_name，是user，递归获取，username是key
      if(val) return val[key];
    }
    //获取user下的具体信息
    return this.getStorage()[key];
  },
  getStorage() {
    //   json字符串传为js对象，获取整个mall下的value
    return JSON.parse(sessionStorage.getItem(STORAGE_KEY|| '{}'))
  },
  clear(key, module_name){
    let val = this.getStorage();
    if (module_name){
      if (!val[module_name])return;
      delete val[module_name][key];
    }else{
      delete val[key];
    }
    window.sessionStorage.setItem(STORAGE_KEY, JSON.stringify(val));
  }
};
